<?php

function list_jabatan_description(){
	$header = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:5%'), 
		'No Posisi'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Nama Jabatan'=>array('style'=>'border-bottom:2px solid;width:25%'),  
		'Tugas pokok'=>array('style'=>'border-bottom:2px solid;width:50%'),   
		' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);
	$facebox=true;
	$key = isset($_GET['key']) ?$_GET['key'] : '';
	$com = isset($_GET['com']) ?$_GET['com'] : '';
	$task = isset($_GET['task']) ?$_GET['task'] : '';
	$field = isset($_GET['field']) ?$_GET['field'] : '';
	if( isset($_GET['key']) ){
		$query = "
			SELECT a.* 
			FROM jabatan a	
			INNER JOIN sk_jabatan b ON b.sk_id=a.sk_id 
			WHERE ( a.nama_jabatan LIKE '%{$key}%' 
			OR a.nomor_posisi ='{$key}' )
			AND b.active='Y' 
			ORDER BY a.urut
		";
	}else{
		$query = "SELECT a.* 
			FROM jabatan a	
			INNER JOIN sk_jabatan b ON b.sk_id=a.sk_id 
			WHERE b.active='Y' 
			ORDER BY a.urut
		" ;
	}
	$result = my_query($query );
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =20;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&field={$_GET['field']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
			$kgPagerOBJ->pager_set(
				$pager_url, 
				$total_records, 
				$scroll_page, 
				$per_page, 
				$current_page, 
				$inactive_page_tag, 
				$previous_page_text, 
				$next_page_text, 
				$first_page_text, 
				$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page);  
	
	$i = ($current_page  - 1 ) * 20; 
	$row = array();

	while($ey = my_fetch_array($result) ){
		$i++;
/*		
		$editproperty = array(
			'href'=>'index.php?com=jabatan_description&task=edit&jabatan_id='.$ey['jabatan_id'] ,
			'rel'=>'facebox',
			'title'=>'Tugas Pokok'
		);	
		$edit_button = button_icon( 'b_props.png' , $editproperty  );
*/
		$tjproperty = array(
			'href'=>'index.php?com=jabatan_description&task=list_tanggung_jawab&jabatan_id='.$ey['jabatan_id'].'&back='.base64_encode( end(explode("?",$_SERVER['REQUEST_URI'])) ),
			'title'=>'Uraian Pekerjaan'
		);
		$tj_button = button_icon( 'b_props.png' , $tjproperty  );

		
		$row[] = array(
			'#'=>position_text_align ($i, 'center') , 
			'nomor'=>  $ey['nomor_posisi'] ,
			'jabatan'=>  $ey['nama_jabatan'] ,
			'tp'=> ( ( strlen($ey['job_summary']) >75 ) ? substr($ey['job_summary'],0,75).' ...' : $ey['job_summary'] ) , 
			'operasi'=> position_text_align( $tj_button , 'right'),  
		);
		
	}
	$datas = table_rows($row);
	$search = '<form method="GET"><input type="hidden" name="com" value="'.$_GET['com'].'" /><span style="font:10px verdana"><b>Pencarian cepat</b></span><br/><input type="text"  style="width:200px;" name="key" value="'.(isset($_GET['key']) ? $_GET['key']: "").'" /><input class="simple_search" type="submit" value=" CARI " /> </form>';
	$menuontop = array(
	//	'Cetak' => array('onclick'=>'javascript:window.open(\'index.php?com=report&task=jabatan_existing\',\'mywin\',\'left=20,top=20,width=800,height=600,toolbar=0,resizable=0\');')
	);
	$printmenu = additional_menu_on_list( $menuontop ,$search ); 
	$paging = $kgPagerOBJ ->showPaging();
	return $printmenu . table_builder($header , $datas ,  5 , false,$paging  ) ;
}

function form_jabatan_description_create($jabatan_id){
	$view = form_header( "jabatan_description" , "jabatan_description"  );
	$fields = my_get_data_by_id('jabatan','jabatan_id',$jabatan_id);
	$view .= form_field_display( $fields['nama_jabatan']   , "Jabatan"   );
	$job_summary = array(
		'name'=>'job_summary',
		'value'=>( isset($_POST['job_summary']) ? $_POST['job_summary'] : $fields['job_summary'] ),
		'id'=>'job_summary', 
		'cols'=>'45',
		'rows'=>'4'
	);
	$form_field_job_summary = form_textarea($job_summary );
	$view .= form_field_display( $form_field_job_summary , "Tugas Pokok"   );
	
 	$submit = array(
		'value' => ' Simpan ',
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit);
	
	$view .= form_field_display( $form_submit, "&nbsp;" ,  "" );
	$view .= form_footer( );	
	return $view;
}

function form_jabatan_description_submit($jabatan_id = 0){
	$datas = array(
		'job_summary'=>my_type_data_str($_POST['job_summary']), 		 	 	 	 	 	 	 
	);
	return my_update_record('jabatan','jabatan_id',$jabatan_id, $datas);
}

function form_jabatan_description_validate($jabatan_id){
	$errsubmit = false;
	$err = array();
	
	$code =  trim($_POST["job_summary"]); 
	if( $code == ''){
		$errsubmit = true;
		$err[] = "Tugas Pokok belum di isi";
	}
	if( $errsubmit){
		return $err;
	}
	return $errsubmit;
}

function list_tanggung_jawab($jabatan_id){
	my_set_code_js('
		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=tanggung_jawab_delete&id=\'+id;
			}
			return false;
		}		
	');	
 
	$header  = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:5%'), 
		'Uraian Pekerjaan'=>array('style'=>'border-bottom:2px solid;width:85%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:10%'),   
	);
	$query = "SELECT * FROM jabatan_tanggung_jawab a
		WHERE a.jabatan_id ={$jabatan_id} ORDER BY jabatan_tanggung_jawab_id ASC";
	$result = my_query($query);
	$i = 0;
	$row = array();
	while($ey = my_fetch_array($result) ){
		$i++;
		$editproperty = array(
				'href'=>'index.php?com=jabatan_description&task=tanggung_jawab_create&jabatan_id='.$jabatan_id.'&id='.$ey['jabatan_tanggung_jawab_id'] ,
				'rel'=>'facebox',
				'title'=>'Edit'
				);	
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );		
		
		$deleteproperty = array(
			'href'=>'javascript:void();', 
			'title'=>'Delete',
			'onclick'=>'javascript:confirmDelete( '.$ey['jabatan_tanggung_jawab_id'].')'
			);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );

		$row[] = array(
			'#'=>position_text_align ($i, 'center'),    
			'tj'=>  $ey['tanggung_jawab'] ,     
			'operasi'=> position_text_align( $edit_button .' '.$delete_button , 'right'),
		);
	}
	$datas = table_rows($row);
	$jabatan =loaddata_jabatan($jabatan_id) ;

	$view .= form_header( "Tanggung Jawab" , "Tanggung Jawab"  );
	$view .= '<tr><td><td><div style="width:100%;text-align:right;">
		<input type="button" value="Tambah Data" onclick="javascript: jQuery.facebox({ ajax: \'index.php?com=jabatan_description&task=tanggung_jawab_create&jabatan_id='.$jabatan_id.'&id=0\' });" class="main_button" /> 
		<input type="button" value="Back" class="main_button" onclick="javascript:location.href=\'index.php?'.base64_decode($_GET['back']).'\'"/>
		</div></td></tr>'   ; 
	$view .= form_field_display( '<span class="label_form">'.$jabatan['nomor_posisi'] .'</span>'  , "<b>Nomor Posisi</b>"    );  	
	$view .= form_field_display( '<span class="label_form">'.$jabatan['nama_jabatan'].'</span>'   , "<b>Jabatan</b>"    );  
	$view .= form_field_display( '<span class="label_form">'.$jabatan['organisasi_departemen'] .'</span>'  , "<b>Departemen</b>"    ); 
	$view .= form_field_display( '<span class="label_form">'.$jabatan['organisasi_direktorat'] .'</span>'  , "<b>Direktorat</b>"    ); 
	$editproperty = array(
		'href'=>'index.php?com=jabatan_description&task=edit&jabatan_id='.$jabatan_id,
		'rel'=>'facebox',
		'title'=>'Tugas Pokok'
	);	
	$edit_button = button_icon( 'b_props.png' , $editproperty  );
	$view .= form_field_display( '<span class="label_form">'. $jabatan['job_summary'].'</span>'  , "<b>Tugas pokok</b> ".$edit_button    ); 
	return  $view. table_builder($header , $datas ,  3 , false );
}

function form_tanggung_jawab_create($jabatan_id, $id=0){
	$fields = my_get_data_by_id('jabatan_tanggung_jawab','jabatan_tanggung_jawab_id',$id);
	$view = form_header( "..." , "..."  );

	$tanggung_jawab = array(
		'name'=>'tanggung_jawab',
		'value'=>( isset($_POST['tanggung_jawab']) ? $_POST['tanggung_jawab'] : $fields['tanggung_jawab'] ),
		'id'=>'tanggung_jawab', 
		'cols'=>'45',
		'rows'=>'4'
	);
	$form_field_tanggung_jawab = form_textarea($tanggung_jawab );
	$view .= form_field_display( $form_field_tanggung_jawab , "Uraian Pekerjaan"   );

	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit);
	
	$view .= form_field_display( $form_submit, "&nbsp;" ,  "" );
	$view .= form_footer( );	
	return $view;
}

function form_tanggung_jawab_submit($jabatan_id, $id= 0){
	$post_tanggung_jawab = stripslashes($_POST['tanggung_jawab']);
	 
	$datas = array(
		'tanggung_jawab'=>my_type_data_str($post_tanggung_jawab), 		 	 	 	 	 	 	 
		'jabatan_id'=>my_type_data_int($jabatan_id), 
	);
  
	if($id > 0){
		return my_update_record('jabatan_tanggung_jawab','jabatan_tanggung_jawab_id',$id, $datas);	
	}
	return my_insert_record('jabatan_tanggung_jawab' , $datas);
}

function form_tanggung_jawab_validate($jabatan_id, $id){
	$errsubmit = false;
	$err = array();
 
	$code =  trim($_POST["tanggung_jawab"]); 
	if( $code == ''){
		$errsubmit = true;
		$err[] = "Uraian Pekerjaan belum di isi";
	}

	$query="SELECT jabatan_tanggung_jawab_id FROM jabatan_tanggung_jawab 
		WHERE tanggung_jawab='".addslashes($code).
		"' AND jabatan_id=".$jabatan_id; 		
		"' AND jabatan_tanggung_jawab_id<>".$id; 
	if (my_num_rows( my_query($query)) >0){
		$errsubmit = true;
		$err[] = "Uraian Pekerjaan sudah digunakan";	
	}
 
	if( $errsubmit){
		return $err;
	}
	return $errsubmit;
}

function delete_tanggung_jawab($id){
	$query = "DELETE FROM jabatan_tanggung_jawab WHERE jabatan_tanggung_jawab_id ={$id} ";
	return my_query($query);
}